<?php

/**
 * @filename: uploads.class.php
 * @usage: Contains all uploading database
 */

class Db_Uploads extends Db_Database {

	/**
	 * @function: __construct
	 * @usage: Upload the podcast or PDF dependant on context
	 */
	public function __construct($post, $files) {
	
		// Create a global database variable
		global $db;
		
		// Create an if statement to see what context we're looking at
		if ($post['submit'] == "Submit Podcast") {
			
			if ($post['title'] == "") {
			
				// If the title is empty, don't bother
				echo "You have not entered a title";
			
			} else {
			
				// Explode the filename to make sure it's mp3 or mp4
				$file = end(explode(".", $files['podcast']['name']));
				
				// Check the file to make sure it's the format we want
				if ($file == "mp3" || $file == "mp4") {
				
					if (file_exists('podcast/'.$files['podcast']['name'])) {
					
						echo "That file already exists";
					
					} else {
					
						// Move the file into the correct directory
						move_uploaded_file($files['podcast']['tmp_name'], 'podcast/'.$files['podcast']['name']);
						
						// Create the database entry
						$db->setQuery("INSERT INTO `uploads` VALUES (NULL, '".$post['title']."', '".$post['description']."', '".$files['podcast']['name']."', 'podcast', '".$_SESSION['realname']."', NOW())");
						
						echo "Upload successful!";
						
					}
				
				} else {
				
					echo "Invalid file";
				
				}
				
			}
		
		} else if ($post['submit'] == "Submit PDF") {
		
			if ($post['title'] == "") {
			
				// If the title is empty, don't bother
				echo "You have not entered a title";
			
			} else {
			
				// Explode the filename to make sure it's a pdf
				$file = end(explode(".", $files['pdf']['name']));
				
				// Check the file to make sure it's the format we want
				if ($file == "pdf") {
				
					if (file_exists('pdf/'.$files['pdf']['name'])) {
					
						echo "That file already exists";
					
					} else {
					
						// Move the file into the correct directory
						move_uploaded_file($files['pdf']['tmp_name'], 'pdf/'.$files['pdf']['name']);
						
						// Create the database entry
						$db->setQuery("INSERT INTO `uploads` VALUES (NULL, '".$post['title']."', '".$post['description']."', '".$files['pdf']['name']."', 'pdf', '".$_SESSION['realname']."', NOW()))");
						
						echo "Upload successful!";
						
					}
				
				} else {
				
					echo "Invalid file";
				
				}
		
		} else {
		
			echo "What are you trying to do to me buddy?";
		
		}
	
	}

}

?>